<?php


namespace app\admin\controller;


use app\admin\model\ShortLink;
use app\admin\service\LinkService;
use think\facade\Request;

class Link extends Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    // 列表
    public function index()
    {
        if (Request::isAjax()) {
            return $this->getJson(ShortLink::getList());
        }
        return $this->fetch();
    }

    // 创建文件夹
    public function add()
    {
        if (Request::isAjax()) {
            return $this->getJson(LinkService::goAdd());
        }
        $expireTime = date('Y-m-d', time() + ($this->global_config['expire_time'] ?? 999) * 86400);
        $this->assign('expireTime', $expireTime);
        return $this->fetch();
    }

    // 编辑
    public function edit($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(LinkService::goEdit());
        }
        $data = ShortLink::find($id);
        $data['expire_time'] = date('Y-m-d', $data['expire_time']);
        return $this->fetch('', ['data' => $data]);
    }

    // 删除
    public function remove()
    {
        return $this->getJson(LinkService::goRemove());
    }
}